package com.ruowei.modules.sys.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class DateUtils {

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 两个日期之间的日期
     *
     * @param begainTime 开始日期
     * @param endTime    结束日期
     * @return 两个日期之间的日期的所有日期
     * @throws Exception 异常捕获
     */
    public static List<String> dateSplit(String begainTime, String endTime)
            throws Exception {
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = formatter.parse(begainTime);
            endDate = formatter.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Long spi = endDate.getTime() - startDate.getTime();
        Long step = spi / (24 * 60 * 60 * 1000);// 相隔天数
        String dateString = formatter.format(startDate);
        List<String> dateList = new ArrayList<>();
        dateList.add(dateString);
        for (int i = 0; i < step; i++) {
            int multiple = i / 24;//倍数
            int mode = i % 24;//余数
            startDate = formatter.parse(dateList.get(multiple * 24));
            dateList.add(formatter.format(new Date(startDate.getTime() + (mode + 1) * (24 * 60 * 60 * 1000))));
        }
        return dateList;
    }

    /*
     * 字符串转Instant(时区为CTT)
     * 自定义格式
     */
    public static Instant toInstant(String dateTimeString, String pattern) {
        return
                LocalDateTime.parse(dateTimeString, DateTimeFormatter.ofPattern(pattern))
                        .atZone(ZoneId.of("Asia/Shanghai"))
                        .toInstant();
    }

    /*
     * 字符串转Instant(时区为CTT)
     * 格式：yyyy-MM-dd hh:mm:ss
     */
    public static Instant toInstant(String dateTimeString) {
        return
                LocalDateTime.parse(dateTimeString, DATE_TIME_FORMATTER)
                        .atZone(ZoneId.of("Asia/Shanghai"))
                        .toInstant();
    }

    /*
     * 字符串转LocalDate(时区为CTT)
     * 格式：yyyy-MM-dd hh:mm:ss
     */
    public static LocalDate toLocalDate(String dateTimeString) {
        return
                LocalDate.parse(dateTimeString, DATE_TIME_FORMATTER);
    }

    /**
     * 获取当前时间的的凌晨时间  如当前时间 2019-08-27 11:28:00的时间戳 通过本方法返回为 2019-08-27 00:00:00时间戳
     *
     * @param instant
     * @return
     */
    public static Instant getDangtianlingche(Instant instant) {
        Calendar cal = new GregorianCalendar();
        cal.setTimeInMillis(instant.toEpochMilli());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Date startDate = cal.getTime();

        Instant dangqianshijian = Instant.ofEpochMilli(startDate.getTime());
        return dangqianshijian;
    }

    public static LocalDateTime toLocalDateTime(String dateTimeString) {
        return
                LocalDateTime.parse(dateTimeString, DATE_TIME_FORMATTER);
    }

    /*
     * 计算日期X和日期Y之间包含多少个周(1,2,3,4,5,6,7)
     */
    public static int dowBetweenTwoDate(LocalDate startDate, LocalDate endDate, Set<DayOfWeek> dayOfWeekSet) {
        int count = 0;
        while (!startDate.isAfter(endDate)) {
            if (dayOfWeekSet.contains(startDate.getDayOfWeek())) {
                count++;
            }
            startDate = startDate.plusDays(1);
        }
        return count;
    }

    public static Date from(Instant instant) {
        try {
            return new Date(instant.toEpochMilli());
        } catch (ArithmeticException ex) {
            throw new IllegalArgumentException(ex);
        }
    }

    /**
     * @param
     * @apiNote 获取今年第一天日期
     * @author 董兴
     * @date 2020-03-19
     */
    public static Date getCurYearFirst() {
        Calendar calendar = Calendar.getInstance();
        int curYear = calendar.get(Calendar.YEAR);
        return getYearFirst(curYear);
    }

    /**
     * @param
     * @apiNote 获取今天最后一天日期
     * @author 董兴
     * @date 2020-03-19
     */
    public static Date getCurYearLast() {
        Calendar calendar = Calendar.getInstance();
        int curYear = calendar.get(Calendar.YEAR);
        return getYearLast(curYear);
    }

    /**
     * @param
     * @apiNote 获取去年第一天日期
     * @author 董兴
     * @date 2020-03-19
     */
    public static Date getLastYearFirst() {
        Calendar calendar = Calendar.getInstance();
        int curYear = calendar.get(Calendar.YEAR);
        return getYearFirst(curYear - 1);
    }

    /**
     * @param
     * @apiNote 获取去年最后一天日期
     * @author 董兴
     * @date 2020-03-19
     */
    public static Date getLastYearLast() {
        Calendar calendar = Calendar.getInstance();
        int curYear = calendar.get(Calendar.YEAR);
        return getYearLast(curYear - 1);
    }

    /**
     * @param year
     * @apiNote 获取某年第一天日期
     * @author 董兴
     * @date 2020-03-19
     */
    public static Date getYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        return calendar.getTime();
    }

    /**
     * @param year
     * @apiNote 获取某年最后一天日期
     * @author 董兴
     * @date 2020-03-19
     */
    public static Date getYearLast(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        return calendar.getTime();
    }

}
